package com.xxl.job.admin;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author xuxueli 2018-10-28 00:38:13
 */
@SpringBootApplication
@MapperScan("com.xxl.job.admin.dao")
public class XxlJobAdminApplication {

	public static void main(String[] args) {
        SpringApplication.run(XxlJobAdminApplication.class, args);
	}
	//todo 指定加载 mapper 路径，和在 application.properties配置中的作用一致，如果配置文件中不好使，这里在启动的时候手动加载
	// ### mybatis
	//#mybatis.mapper-locations=classpath:/mybatis-mapper/*.xml
	@Configuration
	static class MyBatisConfig {

		@Autowired
		private DataSource dataSource;

		@Bean
		public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
			sqlSessionFactoryBean.setDataSource(dataSource);

			// 设置MyBatis相关配置，指定mapper.xml文件位置
			sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
			return sqlSessionFactoryBean;
		}

		private Resource[] resolveMapperLocations() throws IOException {
			ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
			return resolver.getResources("classpath:mybatis-mapper/*.xml");
		}
	}

}